
package com.xebest.web.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xebest.common.enums.erp.order.VenConfirmSendNumEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.util.XeOrderBarUtil;
import com.xebest.web.form.XeVenOrderForm;
import com.xebest.web.mapper.read.XeVenOrderReadMapper;
import com.xebest.web.mapper.read.XeVenPrintsMapper;
import com.xebest.web.mapper.write.XeVenOrderWriteMapper;
import com.xebest.web.model.XeVenOrderInfoEntity;
import com.xebest.web.service.XeVenPrintsService;
import com.xebest.web.view.XeVenSendOrderPrintView;

@Service
public class XeVenPrintsServiceImpl implements XeVenPrintsService {

	@Autowired
	private XeVenPrintsMapper printsMapper;
	@Autowired
	private XeVenOrderWriteMapper orderWriteMapper;
	
	@Autowired
	private XeVenOrderReadMapper xeVenOrderReadMapper;
	
	/**
	 * 查询订单打印发货
	 */
	@Override
	public List<XeVenSendOrderPrintView> querySendOrderPrintList(Map<String, Object> paraMap) {
		List<XeVenSendOrderPrintView> sendOrderPrintList = printsMapper.querySendOrderPrintList(paraMap);
		
		for(XeVenSendOrderPrintView sendOrderPrint : sendOrderPrintList){
			String area = XeBaseUtil.baseUtil.getOrgAreaName(Integer.parseInt(sendOrderPrint.getCaAreaId()));
			sendOrderPrint.setCaStreet(area+sendOrderPrint.getCaStreet());
			sendOrderPrint.setOrderBarCode(XeOrderBarUtil.getBarCode(sendOrderPrint.getOrderId()));
			//生成打印批次号
			//A代表当前订单属于当天哪个波次（取波次号最后一位）
//			String pb = "";
//			if(!StringUtils.isEmpty(sendOrderPrint.getCheckBatchNum())){
//				pb += sendOrderPrint.getCheckBatchNum().substring(sendOrderPrint.getCheckBatchNum().length()-1,
//						sendOrderPrint.getCheckBatchNum().length());
//				pb += "_";
//				List<Map<String,Object>> orderList = orderWriteMapper.selectOrderByCheckBatchNum(sendOrderPrint.getCheckBatchNum());
//				//B代表当前订单所属波次内共有多少个订单
//				pb += orderList.size();
//				pb += "_";
//				//C代表当前订单是所属波次内的第几条订单。
//				for(Map<String,Object> order : orderList){
//					if(order.get("orderId").equals(sendOrderPrint.getOrderId())){
//						pb += String.valueOf(new Double((double) order.get("rownum")).intValue());
//					}
//				}
//			}
//			sendOrderPrint.setPrintBatchNum(pb);
			XeVenOrderForm orderForm = new XeVenOrderForm();
			orderForm.setOrderId(sendOrderPrint.getOrderId());
			orderForm.setUserCode(paraMap.get("userCode").toString());
            Map<String, Object> resultMap = xeVenOrderReadMapper.queryRefundAmount(orderForm);
            if (null != resultMap) {
                if (sendOrderPrint.getConfirmSendNum().equals(VenConfirmSendNumEnum.YES.getValue())) {
                    //有实发差异
                    if (resultMap.containsKey("sfRefundAmount") && StringUtils.isNotBlank(resultMap.get("sfRefundAmount").toString())) {
                        //差异金额
                    	sendOrderPrint.setDiffAmount(new BigDecimal(resultMap.get("sfRefundAmount").toString()));
                    } else {
                        //差异金额
                    	sendOrderPrint.setDiffAmount(new BigDecimal("0"));
                    }
                }
            }
	            
		}
		
		return sendOrderPrintList;  
		
	}

	@Override
	public int updateXeVenOrderInfo(XeVenOrderInfoEntity orderEntity) {
		  
		return orderWriteMapper.updateXeVenOrderInfo(orderEntity);  
	}
	 /**
     * 批量更新订单打印状态
     * @author: 李贝贝
     * @createTime: 2018年1月11日 下午4:07:40  
     * @history:  
     * @param orderList
     * @return int
     */
    public int batchUpdateOrderPrintStauts(List<XeVenSendOrderPrintView> orderList){
    	return orderWriteMapper.batchUpdateOrderPrintStauts(orderList);
    }
}
  
